package cipher.algorithm;

import java.util.ArrayList;

public class LFSR_1 extends Algorithm {
	
	private int registre;
	private int retroaction;

	public LFSR_1(int registre, int retroaction) {
		super();
		this.registre = registre;
		this.retroaction = retroaction;
		setKEY_LENGTH(Integer.toBinaryString(registre).length());
	}
	
	@Override
	public int generateNextNumber() {
		int b = 0;
		int c = registre;

		b = readBit(c, 0);
		
		for(int i=getKEY_LENGTH();--i>=0;)
			if(readBit(retroaction, i) == 1)
				b = b^readBit(c, i);
		
		b = b<<getKEY_LENGTH()-1;
		c = (c>>1) | b;
		
		registre = c;
		return readBit(registre, 0);
	}
	
	@Override
	public int generateNextNumber(int length) {
		int b = 0;
		int c = registre;

		b = readBit(c, 0);
		
		for(int i=getKEY_LENGTH();--i>=0;)
			if(readBit(retroaction, i) == 1)
				b = b^readBit(c, i);
		
		b = b<<getKEY_LENGTH()-1;
		c = (c>>1) | b;
		
		registre = c;
		return readBit(registre, 0);
	}
	
	public ArrayList<String> generateNewKey(int keySize){	return null;	}
	
	public int getRegistre() { return registre; }
	public void setRegistre(int registre) { this.registre = registre; }

	public int getRetroaction() { return retroaction; }
	public void setRetroaction(int retroaction) { this.retroaction = retroaction; }

	@Override
	public String toString() { return Integer.toBinaryString(getRegistre())+Integer.toBinaryString(getRetroaction()); }
	
}
